package ch.ebnoether.xml;

import ch.ebnoether.player.Player;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

public class HighscoreTXT implements Highscore {

    private ArrayList<String> eingelesen;
    private StringBuffer liste;
    private String path;

    public HighscoreTXT() throws IOException {
        this.eingelesen = new ArrayList<String>();
        this.liste = new StringBuffer();
        path = "ch/ebnoether/xml/highscore";
        System.out.println(path);
        readFile();
    }

    @Override
    public void writeFile() {
        sortieren();
        System.out.println(path);

        try {
//            File file1 = new File(YatzyGUI.class.getResource("/ch/ebnoether/controller/highscore").toURI());
            File file1 = new File("/highscore.txt");
            FileOutputStream ausgabe = new FileOutputStream(file1);
            DataOutputStream raus = new DataOutputStream(ausgabe);
            for (int i = 0; i < this.eingelesen.size() && i < 10; i++) {
                raus.writeBytes(this.eingelesen.get(i) + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.toString(), "Error",
                    JOptionPane.ERROR_MESSAGE, null);
        }
    }

    @Override
    public void readFile() {

        try {
            File file1 = new File(path);
//            File file1 = new File(path + "highscore.txt");
            FileReader red = new FileReader(file1);
            this.liste = new StringBuffer();

            for (int c; (c = red.read()) != -1; ) {
                liste.append((char) c);
            }

            StringTokenizer st = new StringTokenizer(liste.toString(), "\n");
            int i = 1;
            while (st.hasMoreTokens()) {
                String str = st.nextToken().toString();
                this.eingelesen.add(str);
                i++;
            }

            // JOptionPane.showMessageDialog(null, this.liste);
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.toString());
        }

    }

    private void sortieren() {
        boolean sortiert = false;
        while (!sortiert) {
            sortiert = true;
            for (int i = 0; i < this.eingelesen.size(); i++) {
                String player1 = this.eingelesen.get(i);
                int score1 = Integer.parseInt(player1.substring(player1
                        .length() - 3));
                for (int j = i + 1; j < this.eingelesen.size(); j++) {
                    String player2 = this.eingelesen.get(j);
                    int score2 = Integer.parseInt(player2.substring(player2
                            .length() - 3));
                    if (score1 < score2) {
                        this.eingelesen.set(i, player2);
                        this.eingelesen.set(j, player1);
                        sortiert = false;
                    }
                }
            }
        }
    }

    @Override
    public void vergleichen(Player player) throws IOException {
//        for (int i = 0; i < this.eingelesen.size() && i < 10; i++) {
//            String string1 = this.eingelesen.get(i);
//            int z = Integer.parseInt(string1.substring(string1.length() - 3));
//            if (z < punkte) {
//                String s = "Gratuliere du hast " + punkte + " Punkte gemacht!"
//                        + "\n\n" + "Du wirst in der Hall of Fame aufgenommen."
//                        + "\n\n" + "Gib Bitte deinen Namen ein.";
//                String str = JOptionPane.showInputDialog(null, s, null);
//                this.eingelesen.add(str + " " + punkte);
//                break;
//            }
//        }
//        sortieren();
//        writeFile();
//        readFile();
//        StringTokenizer temp = new StringTokenizer(this.liste.toString(), "\n");
//        StringBuffer temp2 = new StringBuffer();
//        int i = 1;
//        while (temp.hasMoreTokens()) {
//            temp2.append(i + ". " + temp.nextToken().toString() + "\n");
//            i++;
//        }
//        JOptionPane.showMessageDialog(null, temp2, "Hall of Fame",
//                JOptionPane.INFORMATION_MESSAGE);
    }

    @Override
    public void highScoreAnzeigen() {
        StringTokenizer temp = new StringTokenizer(this.liste.toString(), "\n");
        StringBuffer temp2 = new StringBuffer();
        int i = 1;
        while (temp.hasMoreTokens() && i < 20) {
            if (i < 10) {
                temp2.append("  " + i + ". " + temp.nextToken().toString()
                        + "\n");
                i++;
            } else {
                temp2.append(i + ". " + temp.nextToken().toString() + "\n");
                i++;
            }
        }
        JOptionPane.showMessageDialog(null, temp2, "Hall of Fame",
                JOptionPane.INFORMATION_MESSAGE);

    }
}
